package com.kdg.juc;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by kong on 2017/7/24.  CopyOnWriteArrayList :写入并复制
 * tips:添加操作多时效率低  因为每次添加时都会复制
 *      并发迭代操作多时操作效率高
 */
public class TestCopyOnWriteArrayList {
    public static void main(String[] args) {
        HelloThread helloThread = new HelloThread();
        for (int i=0;i<10;i++){
            new Thread(helloThread).start();
        }
    }
}

class HelloThread implements Runnable{

//    private static List<String> list = Collections.synchronizedList(new ArrayList<String>());
      private static List<String> list = new CopyOnWriteArrayList<String>();
    static{
        list.add("AA");
        list.add("BB");
        list.add("CC");
    }
    @Override
    public void run() {
        Iterator<String> iterator = list.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
            list.remove("AA");
            list.add("TT");
        }
    }
}
